package user

import (
	"gin-project-01/gin-step02/gin11-file-upload-example/controllers"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
)

type UserController struct {
	controllers.BaseController //继承 controllers.BaseController
}

func (u UserController) Index(c *gin.Context) {
	c.String(http.StatusOK, "%s", "用户首页")
}

func (u UserController) Detail(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data":    "tom",
		"success": true,
	})
}

func (u UserController) Delete(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查询成功",
		"data":    nil,
		"success": true,
	})
}

func (u UserController) Add(c *gin.Context) {
	num := rand.Intn(10) + 1
	if num > 5 {
		u.Success(c, nil) //调用父类的Success方法
	} else {
		u.Error(c, "添加用户失败") //调用父类的Error方法
	}
}
